package cn.suxiaoxiang.suwebverify.command;

import cn.suxiaoxiang.suwebverify.SuWebVerify;
import cn.suxiaoxiang.suwebverify.config.MessageConfig;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;

import java.util.ArrayList;

import java.util.List;

import static cn.suxiaoxiang.suwebverify.SuWebVerify.main;

/**
 * defaultCommand - 插件指令
 *
 * @author 程序员-Su_xiaoxiang
 * @date 2025/4/7 21:21
 */
public class defaultCommand implements CommandExecutor, TabExecutor, Listener {
    private final SuWebVerify plugin;
    private final MessageConfig messageConfig;
    private final SetPasswordCommand setPasswordCommand;

    public defaultCommand(SuWebVerify plugin) {
        this.plugin = plugin;
        this.messageConfig = new MessageConfig(plugin);
        this.messageConfig.loadPlayerCommandConfig();
        this.setPasswordCommand = new SetPasswordCommand(plugin);
    }
    //获取插件前缀
    String prefix = main.getConfig().getString("prefix");
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        String version = SuWebVerify.main.getDescription().getVersion();

        // 检查是否有管理员权限
        if (!sender.hasPermission("suwebverify.admin")) {
            sender.sendMessage(ChatColor.RED +prefix+ messageConfig.getPlayerCommandConfig().getString("NoPermission"));
            return false;
        }

        if (args.length == 0) {
            sender.sendMessage(ChatColor.WHITE + ">> " + ChatColor.AQUA + "作者介绍" + ChatColor.WHITE + " <<");
            sender.sendMessage(ChatColor.GREEN + "-----------------------------------------");
            sender.sendMessage(ChatColor.AQUA + "版本: " + ChatColor.WHITE + version);
            sender.sendMessage(ChatColor.AQUA + "作者: " + ChatColor.WHITE + "程序员_Suxiaoxiang");
            sender.sendMessage(ChatColor.GREEN + "-----------------------------------------");
            sender.sendMessage(ChatColor.AQUA + "感谢使用本插件！");
            sender.sendMessage(ChatColor.WHITE + ">> " + ChatColor.AQUA + "指令界面" + ChatColor.WHITE + " <<");
            sender.sendMessage(ChatColor.YELLOW + "--------------------");
            sender.sendMessage(ChatColor.YELLOW + "/swv reload" + " " + ChatColor.DARK_GREEN + "重载插件");
            sender.sendMessage(ChatColor.YELLOW + "/swv setpassword <玩家名> <新密码>" + " " + ChatColor.DARK_GREEN + "设置玩家密码");
            sender.sendMessage(ChatColor.YELLOW + "--------------------");
        } else if (args.length >= 1) {
            switch (args[0].toLowerCase()) {
                case "reload":
                    if (!sender.hasPermission("suwebverify.reload")) {
                        sender.sendMessage(ChatColor.RED + prefix+ messageConfig.getPlayerCommandConfig().getString("NoPermission"));
                        return false;
                    }
                    plugin.reloadConfig();
                    messageConfig.reloadPlayerCommandConfig();
                    sender.sendMessage(ChatColor.GREEN + prefix+"插件重载成功！");
                    break;

                case "setpassword":
                    // 移除第一个参数（setpassword），只传递玩家名和新密码
                    String[] passwordArgs = new String[args.length - 1];
                    System.arraycopy(args, 1, passwordArgs, 0, args.length - 1);
                    return setPasswordCommand.onCommand(sender, command, label, passwordArgs);

                default:
                    sender.sendMessage(ChatColor.RED + prefix +"未知命令！使用 /swv 查看帮助。");
                    break;
            }
        }
        return true;
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
        List<String> completions = new ArrayList<>();

        if (args.length == 1) {
            // 添加子命令
            completions.add("reload");
            completions.add("setpassword");
        } else if (args.length == 2 && "setpassword".equalsIgnoreCase(args[0])) {
            // 返回所有在线玩家的名字
            for (Player player : Bukkit.getOnlinePlayers()) {
                completions.add(player.getName());
            }
        }

        return completions;
    }
}